// /pages/order/component/order-item/index.js
const { decimalInputRegular, integerInputRegular } = require('../../../../utils/util.js');

Component({
  properties:{
    item:{
      type: Object,
      value: ''
    },
    index:{
      type: Number,
      value: 0
    },
    additionalPrice:{
      type: Boolean,
      value: 0
    }
  },

  lifetimes:{
    attached(){
      console.log('order-item，组件attached')
    },
  },

  pageLifetimes:{
    show(){
      console.log('order-item，页面onShow')
    },
    hide(){
      console.log('---order-item---: on hide')
    },
    onReady(){
      console.log('---order-item---: onReady')
    }
  },
  /**
   * 页面的初始数据
   */
  data: {
    dynamicColor: 'white',
  },

  methods:{
    handleMinus(e){
      this.triggerEvent('quantity-minus', {
        ...e.detail, 
        index: this.properties.index
      })
    },
    handlePlus(e){
      this.triggerEvent('quantity-plus', {
        ...e.detail, 
        index: this.properties.index
      })
    },
    handleDelete(e){
      this.triggerEvent('quantity-delete', {
        ...e.detail, 
        index: this.properties.index
      })
    },
    onInputHandler(e){
      var orgValue = e.detail.value;
      var {valueText, value} = integerInputRegular(orgValue);
      console.log('输入实时校准后的值: ', valueText, value)
      this.setData({
        'item.count': value
      })

      this.triggerEvent('quantity-input', {
        ...e.detail,
        value,
        index: this.properties.index
      })
    },

    handleDecimalInput(e){
      console.log('order-item:小数输入框', e)
      var orgValue = e.detail.value;
      var {valueText, value} = decimalInputRegular(orgValue);
      console.log('输入实时校准后的值: ', valueText, value)
      this.setData({
        'item.count': value
      })

      this.triggerEvent('quantity-input', {
        ...e.detail,
        value,
        index: this.properties.index
      })
    },

    onInputFocusHandler(event){
      const index = event.currentTarget.dataset.index; // 获取索引
      this.setData({
        dynamicColor: 'whitesmoke'
      })
    },

    onInputBlurHandler(event){
      const index = event.currentTarget.dataset.index; // 获取索引
      this.setData({
        dynamicColor: 'white'
      })
    },
  }
})